package com.stx.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.stx.dao.MaintainingDao;
import com.stx.dao.impl.BuildDaoImpl.rowMapper;
import com.stx.dao.impl.TeacherDaoImpl.info;
import com.stx.domain.Building;
import com.stx.domain.Maintaining;
import com.stx.domain.Student;
@Repository
public class MaintainingDaoImpl implements MaintainingDao {

	@Resource
	private JdbcTemplate jt;
	
	@Override
	public void addMaintaining(Maintaining main) {
		// TODO Auto-generated method stub
		Date date=new Date();
		  DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  String time=format.format(date);
		String sql="insert into addmaintaining values(id.nextval,?,?,?,?,?,?)";
		Object[] obj={time,main.getNo(),main.getName(),main.getD(),main.getThing(),main.getRemark()};
		jt.update(sql,obj);
	}
	@Override
	public List<Maintaining> selectMaintaining(String d) {
		// TODO Auto-generated method stub
	
		String sql="select * from addmaintaining where d='"+d+"'";
		List<Maintaining> list=jt.query(sql, new info());
		
		return list;
	}

	
	


	@Override
	public void deleteMaintaining(int id) {
		// TODO Auto-generated method stub
		String sql="delete from addmaintaining where id="+id+"";
		jt.update(sql);
	}
	@Override
	public List<Maintaining> selectMaintainingByT() {
		// TODO Auto-generated method stub
		String sql="select * from addmaintaining";
		List<Maintaining> list=jt.query(sql, new info());
		return list;
	}
	


	@Override
	public void deleteMaintainingByT(int id) {
		// TODO Auto-generated method stub
		String sql="delete from addmaintaining where id ='"+id+"'";
		jt.update(sql);
	}
	@Override
	public Maintaining selectMaintaining(int id) {
		// TODO Auto-generated method stub
		String sql = "select * from addmaintaining where id='"+id+"'";
		List<Maintaining> list = jt.query(sql, new info());
		if(list.size()==1)
			return  list.get(0);
		return null;
	}
	
	class info implements RowMapper{
		public Object mapRow(ResultSet rs, int arg1) throws SQLException {
	
			Maintaining m=new Maintaining();
			m.setId(rs.getInt("id"));
			m.setTime(rs.getString("time"));
			m.setNo(rs.getShort("no"));
			m.setName(rs.getString("name"));
			m.setD(rs.getString("d"));
			m.setThing(rs.getString("thing"));
			m.setRemark(rs.getString("remark"));
			
			System.out.println(m.getTime());
			return m;
		}
		
	}
}
